// Released in the Public Domain. http://creativecommons.org/licenses/publicdomain/
package org.xi8ix.async.spi;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;

/**
 * A peer for datagram servers. A datagram peer cannot stand alone and must defer to
 * it's server (and the server's DatagramChannel) for sending data.
 *
 * @author Iain Shigeoka
 */
public class DatagramPeer extends AbstractPeer {
    /** The peer's server which controls the UDP DatagramChannel used to send and receive data for the peer. */
    private DatagramServer server;

    /**
     * Create a peer for the given server.
     *
     * @param address the address used to talk to this peer
     * @param server the server that owns this peer virtual connection
     */
    public DatagramPeer(InetSocketAddress address, DatagramServer server) {
        super(address);
        this.server = server;
    }

    /**
     * Sends the data using this peer's DatagramServer.
     *
     * @param dst the destination for the data.
     * @param data the data to send.
     * @return a future used to query the status of the send operation and optionally cancel it.
     */
    public Future<Boolean> send(InetSocketAddress dst, ByteBuffer data) {
        return server.send(dst, data);
    }

    /**
     * Does nothing, this is a virtual connection.
     */
    public void close() {
    }
}
